package com.att.action;

import com.adkfp.utils.LayuiUtil;
import com.att.bean.UserSpedayBean;
import com.att.service.UserSpedayService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("userSpeday")
public class UserSpedayAction {

    @Resource(name = "userSpedayService")
    private UserSpedayService userSpedayService;

    /**
     * Description: 带条件查询公出请假记录
     * @param bean
     */
    @RequestMapping("selectAll")
    @ResponseBody
    public Object selectAll(UserSpedayBean bean) {
        List<UserSpedayBean> list = userSpedayService.findByCondition(bean);
        long count = userSpedayService.findByConditionCount(bean);
        return LayuiUtil.data(count, list);
    }

    /**
     * Description: 转发到页面
     */
    @RequestMapping("userSpedayIndex")
    public String userSpedayIndex() {
        return "check/userSpeday";
    }

    //新增
    @RequestMapping("insert")
    @ResponseBody
    public String insert(UserSpedayBean bean) {
        bean.setCreateTime(new Date());
        String[] idList = bean.getUserId().split(",");
        for (int j = 0; j < idList.length; j++) {
            String id = idList[j];
            bean.setUserId(id);
            boolean flag = userSpedayService.insert(bean);
            if (!flag) {
                return "ERROR";
            }
        }
        return "SUCCESS";
    }

    //修改
    @RequestMapping("update")
    @ResponseBody
    public String update(UserSpedayBean bean) {
        boolean flag = userSpedayService.update(bean);
        if(flag){
            return "SUCCESS";
        }
        return "ERROR";
    }

    //删除/批量删除
    @RequestMapping("delete")
    @ResponseBody
    public String delete(String id) {
        System.out.println("MCM----------"+id);
        boolean flag = userSpedayService.delete(id);
        if(flag){
            return "SUCCESS";
        }
        return "ERROR";
    }
}
